package Operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-09-21
 * Time:21:06
 */
public class DelOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除图书的名字");
        String name = scanner.nextLine();
        int size = bookList.getBookSize();
        int pos = -1;
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBook(i);

            Book book1 = bookList.getBook(size - 1);
            if (book.getName().equals(name)){
                pos = i;
                break;
            }
        }
        if (pos != -1){
            for (int i = pos; i < size - 1; i++) {
                Book book = bookList.getBook(i + 1);
                bookList.setBook(i,book);
            }
            bookList.setBookSize(size - 1);
            System.out.println("删除成功");
        }else {

            System.out.println("没有找到该图书");
        }
    }
}
